
package xib.core;


import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import xib.Panel;
import xib.parserTools.interfaces.IPanelData;
import xib.parserTools.interfaces.ISpeechBubbleData;
import xib.utils.XibIOFileException;

/**
 * This class contains the information from a specific Panel.
 * @author xavierds
 */
public final class PanelXib implements Panel{

    /**
     * Field with the value of the imageIdentifier.
     */
    private String imageIdentifier;

    /**
     * Field with the location from the X axis.
     */
    private int locationX;

    /**
     * Field with the location from the Y axis.
     */
    private int locationY;

    /**
     * Field with the height of the panel.
     */
    private int height;

    /**
     * Field with the width of the panel.
     */
    private int width;

    /**
     * Field with the relative order of the panel.
     */
    private int order;

    /**
     * List of languages of the differents SpeechBubbles.
     */
    private ArrayList<String> languages;

    /**
     * List of speechBubbles in a specific languages.
     */
    private ArrayList<ArrayList<SpeechBubbleXib>> speechBubbles;


    /**
     * Field with the information about the InputStream
     */
    private InputStream stream;

    private FileHandler fh;

    /**
     * Default Constructor
     * @param panel must be correctly initialized.
     */
    public PanelXib(IPanelData panel){

        //First step we validate the panelData.
        panel.validate();

        height = panel.getHeight();
        width = panel.getWidth();
        imageIdentifier = panel.getImageIdentifier();
        locationX = panel.getLocationX();
        locationY = panel.getLocationY();
        order = panel.getOrder();
        languages = panel.getLanguages();
        speechBubbles = new ArrayList<ArrayList<SpeechBubbleXib>>();

        ArrayList<ISpeechBubbleData> list;
        ArrayList<SpeechBubbleXib> list2;

        int n = languages.size();
        int m;
        SpeechBubbleXib speechBubble;
        for(int i=0; i < n; i++){
            list = panel.getSpeechBubblesDataByLanguage(languages.get(i));
            m = list.size();
            list2 = new ArrayList<SpeechBubbleXib>();
            for(int j=0; j < m; j++){
                speechBubble = new SpeechBubbleXib(list.get(j));
                list2.add(speechBubble);
            }
            speechBubbles.add(list2);
        }
    }

    /**
     * Returns the identifier from the image
     * @return string
     */
    public String getImageIdentifier(){
        return imageIdentifier;
    }

    /**
     * Returns the position from the x axis.
     * @return integer
     */
    public int getLocationX(){
        return locationX;
    }

    /**
     * Returns the position from the y axis.
     * @return integer
     */
    public int getLocationY(){
        return locationY;
    }

    /**
     * Returns the height of the panel.
     * @return integer
     */
    public int getHeight(){
        return height;
    }

    /**
     * Returns the width of the panel.
     * @return integer
     */
    public int getWidth(){
        return width;
    }

    /**
     * Returns the order of the panel.
     * @return integer
     */
    public int getOrder(){
        return order;
    }

    
    public ArrayList<SpeechBubbleXib> getSpeechBubblesXib(String language){
        int size = languages.size();
        for(int i=0; i < size; i++)
            if(language.equals(languages.get(i)))
                return speechBubbles.get(i);
        return null;
    }

    
    void setInputStream(FileHandler fh){
        this.fh = fh;
        try{
            stream =  fh.getEntry(imageIdentifier);
            ArrayList<SpeechBubbleXib> list;
            for(int i=0; i < speechBubbles.size(); i++){
                list = speechBubbles.get(i);
                for(int j=0; j < list.size(); j++){
                    list.get(j).setInputStream(fh);
                }
            }
        }
        catch(IOException ex){
            throw new XibIOFileException("The panel " + imageIdentifier +
                    " wasn't load correctly", ex);
        }
    }

    public InputStream getInputStream(){
        InputStream aux = stream;
        setInputStream(fh);
        return aux;
    }
}
